import 'package:distribution_mall_flutter/entity/order/order.dart';
import 'package:distribution_mall_flutter/http/modules/order.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class OrderCommonState {
  List<SaleReasonEntity> reasonList;
  String value;
  bool loading;

  OrderCommonState(
      {this.reasonList = const [], this.value = '', this.loading = false});

  OrderCommonState.initial()
      : reasonList = const [],
        value = '',
        loading = false;

  OrderCommonState copyWith(
          {List<SaleReasonEntity>? reasonList, String? value, bool? loading}) =>
      OrderCommonState(
          reasonList: reasonList ?? this.reasonList,
          value: value ?? this.value,
          loading: loading ?? this.loading);
}

final orderCommonProvider =
    StateNotifierProvider.autoDispose<OrderCommonViewModal, OrderCommonState>(
        (ref) {
  return OrderCommonViewModal();
});

class OrderCommonViewModal extends StateNotifier<OrderCommonState> {
  OrderCommonViewModal() : super(OrderCommonState.initial());

  getAfterSaleReason() async {
    List<SaleReasonEntity>? data = (await OrderApi.getAfterSaleReason()).data;
    if (data == null) return;
    state = state.copyWith(reasonList: data);
  }

  setValue(String value) {
    state = state.copyWith(value: value);
  }

  Future<bool> orderCancel(Map<String, dynamic> params) async {
    state = state.copyWith(loading: true);
    try {
      return await OrderApi.orderCancel(
        params,
      );
    } catch (e) {
      return false;
    } finally {
      state = state.copyWith(loading: false);
    }
  }
}
